package App.OO.service;

import App.Model.Interface.MessageType;
import App.Model.User;
import App.Model.message.Message;
import App.OO.ClientView.OOMainFrame;
import App.OO.service.thread.CtoSThread;
import App.OO.service.thread.Manage.ManageCtoSThread;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class OOLoginSignService {
    private User user = new User();
    private Socket socket;

    public int checkUser(String userId, String pwd) {//验证user合法性
        //创建与服务器的链接是先传User后收Message,有了json后可以直接传Message,简化程序
        int loginState = 0;
        user.setUserId(userId);
        user.setPassword(pwd);
        //连接到服务端

        try {
            socket = new Socket(InetAddress.getByName("10.20.209.199"), 9999);
        } catch (IOException e) {
            System.out.println("服务器未启动");
            return 0;
        }
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            //发送user到服务端
            outputStream.writeObject(user);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            //读取从服务端回复的Message信息
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) inputStream.readObject();

            if (message.getMesType().equals(MessageType.MESSAGE_LOGIN_SUCCEED)) {//登录成功
                loginState = 1;
                //创建一个和服务器端始终保持联系的线程与该线程调动的窗口
                OOMainFrame mainFrame = new OOMainFrame(this);
                mainFrame.showMainFrame();//打开窗口
                CtoSThread ctosThread = new CtoSThread(socket, mainFrame);
                ctosThread.start();
                ManageCtoSThread.addCtoSThread(userId, ctosThread);

            } else if(message.getMesType().equals(MessageType.MESSAGE_LOGIN_FAIL)){
                //登录失败(密码错误),关闭socket
                loginState = -1;
                socket.close();
            } else if(message.getMesType().equals(MessageType.MESSAGE_LOGIN_FAIL_LOGGED)){
                //登陆失败(已有用户登录)
                loginState = -2;
                socket.close();
            }
            else {
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return loginState;
    }

    //请求用户列表,后续考虑是否重构到其他service中
    public void getUserAndOnlineList() {
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_GET_ONLINE_LIST);
        message.setSender("服务器");

        //线程->内置socket->对应的输入输出流
        CtoSThread ctoSThread = ManageCtoSThread.getCtoSThread(user.getUserId());//若为新用户,会因为user未初始化而报错
        try {
            ObjectOutputStream oos = new ObjectOutputStream(ctoSThread.getSocket().getOutputStream());
            //发送请求
            oos.writeObject(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void exitClient() {//二级菜单时退出操作
        //包装
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_EXIT);
        message.setSender(user.getUserId());

        //发送
        try {
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(message);
            System.out.println("退出系统");
            //关闭ManageCtoSThread中对应用户的CtoSThread
            CtoSThread close_CtoSThread = ManageCtoSThread.getCtoSThread(user.getUserId());
            close_CtoSThread.notStop = false;
            close_CtoSThread.getSocket().close();
            System.exit(0);
        } catch (Exception e) {
            //e.printStackTrace();//成品应该将报错关闭?
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }
}
